var axios = require('axios')
var config = require('../config')


var a_url = config.get('a_url')
var app_url = config.get('app_url')


const aurl = axios.create({
    baseURL:a_url,
    timeout:5000,
})
const appurl = axios.create({
    baseURL:app_url,
    timeout:5000,
})
var load = document.getElementById('loading')
aurl.interceptors.request.use(config=> {
    load ? load.className = 'block' : ''
    return config;
});
aurl.interceptors.response.use(response=> {
    load ? load.className = 'loadnone' : ''
    return response;
});
appurl.interceptors.request.use(config=> {
    load ? load.className = 'block' : ''
    return config;
});
appurl.interceptors.response.use(response=> {
    load ? load.className = 'loadnone' : ''
    return response;
});
//手机号 密码获取用户信息
function getToken(phone,pwd,callback){
    const params = new URLSearchParams()
    params.append('username',phone);
    params.append('password', pwd);
    params.append('sertype', 1);
    appurl.post(`/index/index/${config.get('v')}_login`,params).then(res=>{
    //appurl.post('/index/index/login',params).then(res=>{
        if (res.data.code == 200) {
            callback(false,res.data.data)
        } else {
            callback(true,res.data.data)
        }
    })
}

//发送验证码
function sendCode(phone,callback){
    aurl.get('cgetTelCodeToLogin?tel='+phone+'&sertype=0')
        .then(res=>{
            if (res.data.code == 200) {
                callback(false,"发送成功")
            } else {
                callback(true,res.data.data)
            }
        })
}

//手机号 验证码获取token
function codeGetToken(phone,code,callback){
    aurl.get('cTelCodeLogin?tel='+phone+'&sertype=1&code='+code)
        .then(res=>{
            if (res.data.code == 200) {
                axios.get(app_url+'/index/index/login?key='+res.data.data.key)
                .then(res=>{
                    if (res.data.code == 200) {
                        callback(false,res.data.data)
                    } else {
                        callback(true,res.data.data)
                    }
                })
            } else {
                callback(true,res.data.data)
            }
        })
}

//切登录
function keyGetKey(key,type){
    // var urlList = ['http://deal.mingyukehui.com/home']
    console.log(type)
    var sertype = config.get('sertype')
    switch (type) {
        case 0:
            aurl.get('checkoutLoginType?key='+key+'&sertype='+sertype)
            .then(res=>{
                console.log(res)
                // window.location = 'http://deal.mingyukehui.com/home?key='+res.data.data.key
            })
            break;
        case 1:
            aurl.get('checkoutLoginType?key='+key+'&sertype='+sertype)
            .then(res=>{
                console.log(res)
                window.location = 'http://deal.mingyukehui.com/home?key='+res.data.data.key
            })
            break;
        case 2:
            aurl.get('checkoutLoginType?key='+key+'&sertype='+sertype)
            .then(res=>{
                console.log(res)
                window.location = 'http://mingyukehui.com/login?key='+res.data.data.key
            })
            break;
        case 4:
            aurl.get('checkoutLoginType?key='+key+'&sertype='+sertype)
            .then(res=>{
                console.log(res)
                // window.location = 'http://deal.mingyukehui.com/home?key='+res.data.data.key
            })
            break;
        default:
            break;
    }
    
}

//url带key 获取用户信息
function keyGetToken(key,type,callback){
    aurl.get('checkoutLoginType?key='+key+'&sertype='+type)
        .then(res=>{
            if (res.data.code == 200) {
                axios.get(app_url+'/index/index/login?key='+res.data.data.key)
                .then(res=>{
                    if (res.data.code == 200) {
                        callback(false,res)
                    }
                })
            } else {
                callback(true,res.data.data)
            }
        })
}

//导出
module.exports = {
    getToken,
    sendCode,
    codeGetToken,
    keyGetToken,
    keyGetKey,
}